#include <amxmodx>
#include <amxmisc>
#include <dprp>

new g_JobIds[512]
new g_JobNum

new g_MenuPage[33]

new g_MainMenu[] = "DPRP_JobFairMenu"
new g_ListMenu[] = "DPRP_JobFairList"
new g_AcceptMenu[] = "DPRP_AcceptMenu"

new g_Menu[512]

new g_JobPending[33]

public plugin_init()
{
	new Version[33]
	DPRP_Version(Version,32)
	register_plugin("DeepPurpleRP - Job Fair",Version,"Hawk552")
	
	register_clcmd("say /help","CmdHelp")
	
	register_menucmd(register_menuid(g_MainMenu),1023,"MainMenuHandle")
	register_menucmd(register_menuid(g_ListMenu),1023,"ListMenuHandle")
	register_menucmd(register_menuid(g_AcceptMenu),1023,"AcceptMenuHandle")
}

public DPRP_Error(const Reason[])
	pause("d")

public CmdHelp(id)
{
	copy(g_Menu,511,"DeepPurpleRP Help^n^n1. Job Listing^n2. Help Commands^n3. Items List^n^n0. Exit")
	show_menu(id,MENU_KEY_1|MENU_KEY_2|MENU_KEY_3|MENU_KEY_0,g_Menu,-1,g_MainMenu)
	
	return PLUGIN_HANDLED
}

public MainMenuHandle(id,Key)
	switch(Key)
	{
		case 0 :
		{
			new Pos,Num,JobName[33],Keys = (1<<7|1<<8|1<<9)
			Pos += format(g_Menu,511,"DeepPurpleRP Job Listing^n^n")
			for(new Count = g_MenuPage[id] * 7;Count < g_MenuPage[id] * 7 + 7;Count++)
			{
				if(Count >= g_JobNum)
					break
				
				if(!DPRP_ValidJobId(g_JobIds[Count]))
					continue
					
				Keys |= (1<<Num++)
				DPRP_GetJobName(g_JobIds[Count],JobName,32)
				Pos += format(g_Menu[Pos],511 - Pos,"%d. %s^n",Num,JobName)
			}
			format(g_Menu[Pos],511 - Pos,"^n8. Last Page^n9. Next Page^n^n0. Exit")
			
			show_menu(id,Keys,g_Menu,-1,g_ListMenu)
		}
		case 1 :
		{
			client_cmd(id,"dprp_help")
			client_print(id,print_chat,"[DPRP] The command listing has been displayed in your console.")
		}
		case 2 :
		{
			client_cmd(id,"dprp_itemlist")
			client_print(id,print_chat,"[DPRP] The item list has been displayed in your console.")
		}
	}

public ListMenuHandle(id,Key)
{
	if(Key == 9)
		return
	
	if(Key == 8)
	{
		if(g_MenuPage[id] * 7 + 7 < g_JobNum)
			g_MenuPage[id]++
		
		MainMenuHandle(id,0)
		
		return
	}
	if(Key == 7)
	{
		if(g_MenuPage[id])
			g_MenuPage[id]--
		
		MainMenuHandle(id,0)
		
		return
	}
	
	new JobId = g_MenuPage[id] * 7 + Key,JobName[33],Access[JOB_ACCESSES + 1]
	DPRP_IntToAccess(DPRP_GetJobAccess(g_JobIds[JobId]),Access,JOB_ACCESSES)
	DPRP_GetJobName(g_JobIds[JobId],JobName,32)
	
	g_JobPending[id] = g_JobIds[JobId]

	format(g_Menu,511,"DeepPurpleRP Job Listing^n^nJob Name: %s^nSalary: $%d/h^nAccess: %s^n^n1. Accept^n2. Deny",JobName,DPRP_GetJobSalary(g_JobIds[JobId]),Access)
	show_menu(id,MENU_KEY_1|MENU_KEY_2,g_Menu,-1,g_AcceptMenu)
}	

public AcceptMenuHandle(id,Key)
	if(!Key)
	{
		DPRP_SetUserJobId(id,g_JobPending[id])
		client_print(id,print_chat,"[DPRP] You have accepted this job.")
	}
	else
		MainMenuHandle(id,0)
	
public DPRP_Init()
	set_task(5.0,"GetJobs")
	
public GetJobs()
{
	new Filename[128]
	DPRP_GetConfigsdir(Filename,127)
	
	add(Filename,127,"/jobfair.ini")
	
	new File = fopen(Filename,"r")
	if(!File)
		set_fail_state("Could not find job fair file for this map.")
	
	while(!feof(File))
	{
		// using Filename as cache; don't let the name deceive you
		fgets(File,Filename,127)
		remove_quotes(Filename)
		trim(Filename)
		replace(Filename,127,"^n","")
		
		new JobId[1]
		DPRP_FindJobId(Filename,JobId,1)
		
		if(JobId[0])
			g_JobIds[g_JobNum++] = JobId[0]
	}
	
	fclose(File)
}
